package src.main.java.com.huawei.demo;

import com.huaweicloud.sdk.core.auth.GlobalCredentials;
import com.huaweicloud.sdk.core.exception.ClientRequestException;
import com.huaweicloud.sdk.core.http.HttpConfig;
import com.huaweicloud.sdk.core.utils.StringUtils;
import com.huaweicloud.sdk.iam.v3.IamClient;
import com.huaweicloud.sdk.iam.v3.model.CreateTokenWithIdTokenRequest;
import com.huaweicloud.sdk.iam.v3.model.CreateTokenWithIdTokenResponse;
import com.huaweicloud.sdk.iam.v3.model.GetIdTokenAuthParams;
import com.huaweicloud.sdk.iam.v3.model.GetIdTokenIdScopeBody;
import com.huaweicloud.sdk.iam.v3.model.GetIdTokenIdTokenBody;
import com.huaweicloud.sdk.iam.v3.model.GetIdTokenRequestBody;
import com.huaweicloud.sdk.iam.v3.model.GetIdTokenScopeDomainOrProjectBody;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;

public class Application {
    private static final Logger logger = LoggerFactory.getLogger(Application.class);

    public static void main(String[] args) {
        String ak = ""; // ak,必传
        String sk = ""; // sk,必传
        String domainId = ""; // 租户id:domainId,必传

        // 配置客户端属性
        HttpConfig config = HttpConfig.getDefaultHttpConfig();
        config.withIgnoreSSLVerification(true);

        // 创建认证
        GlobalCredentials auth = new GlobalCredentials()
            .withAk(ak)
            .withSk(sk)
            .withDomainId(domainId);

        // 创建请求客户端
        ArrayList<String> endpoints = new ArrayList<String>();
        endpoints.add(""); // 固定值，填入：https://iam.myhuaweicloud.com
        IamClient client = IamClient.newBuilder()
            .withCredential(auth)
            .withEndpoints(endpoints)
            .build();

        // 获取联邦认证token(OpenID Connect ID token方式)
        createTokenWithIdToken(client);
    }

    private static void createTokenWithIdToken(IamClient client) {
        String xIdpId = ""; // 身份提供商Id。必传
        String idToken = ""; // id_token信息。必传

        // 获取unscoped token，下面四个参数都不传，否则根据下面条件传入
        String scopeDomainId = ""; // 获取联邦认证project scoped token，同时填入projectId和projectName
        String scopeDomainName = "";
        String scopeProjectId = ""; // 获取联邦认证domain scoped token，同时填入projectId和projectName
        String scopeProjectName = "";

        // 构造请求
        CreateTokenWithIdTokenRequest request = new CreateTokenWithIdTokenRequest(); // 请求对象
        GetIdTokenAuthParams authbody = new GetIdTokenAuthParams(); // authbody用于存放各种请求体参数

        // 设置scope信息
        GetIdTokenIdScopeBody scopeAuth = new GetIdTokenIdScopeBody(); // scope对象
        if (!StringUtils.isEmpty(scopeProjectId) && !StringUtils.isEmpty(scopeProjectName)) {
            GetIdTokenScopeDomainOrProjectBody projectScope = new GetIdTokenScopeDomainOrProjectBody();
            projectScope.withId(scopeProjectId).withName(scopeProjectName); // scope为project，设置projectId和projectName
            scopeAuth.withProject(projectScope);
            authbody.withScope(scopeAuth); // 设置scope
        } else if (!StringUtils.isEmpty(scopeDomainId) && !StringUtils.isEmpty(scopeDomainName)) {
            GetIdTokenScopeDomainOrProjectBody domainScope = new GetIdTokenScopeDomainOrProjectBody();
            domainScope.withId(scopeDomainId).withName(scopeDomainName);
            scopeAuth.withDomain(domainScope);
            authbody.withScope(scopeAuth); // 设置scope
        }

        request.withXIdpId(xIdpId); // 请求头设置X-Idp-Id
        GetIdTokenIdTokenBody idTokenAuth = new GetIdTokenIdTokenBody();
        idTokenAuth.withId(idToken);
        authbody.withIdToken(idTokenAuth); // 设置id_token
        GetIdTokenRequestBody body = new GetIdTokenRequestBody(); // 请求体对象
        body.withAuth(authbody);
        request.withBody(body);

        try {
            // 执行请求
            CreateTokenWithIdTokenResponse response = client.createTokenWithIdToken(request);
            logger.info(response.toString());
        } catch (ClientRequestException e) {
            LogError(e);
        }
    }

    private static void LogError(ClientRequestException e) {
        logger.error("HttpStatusCode: " + e.getHttpStatusCode());
        logger.error("RequestId: " + e.getRequestId());
        logger.error("ErrorCode: " + e.getErrorCode());
        logger.error("ErrorMsg: " + e.getErrorMsg());
    }
}
